import { queryDictData, queryPerson } from '@/utils/common';
import AdSelect from '@/components/AdSelect';
import AdSearch from '@/components/AdSearch';
import router from 'umi/router';

export const formate = 'YYYY-MM-DD HH:mm:ss';
export const formateNOSecond = 'YYYY-MM-DD HH:mm';

export const declareStatus = [
  { code: 'DRAFT', value: '草稿' },
  { code: 'COMMIT', value: '已提交' },
];


export const codes = {
  select: 'ROADMANIFESTPAGE_SELECT',
  add: 'ROADMANIFESTPAGE_ADD',
  showDetail: 'ROADMANIFESTPAGE_VIEW',
  page: 'ROADMANIFESTPAGE',
  commit: 'ROADMANIFESTPAGE_COMMIT',

  selectPlus: 'ROADMANIFESTPAGEPLUS_SELECT',
  addPlus: 'ROADMANIFESTPAGEPLUS_ADD',
  showDetailPlus: 'ROADMANIFESTPAGEPLUS_VIEW',
  pagePlus: 'ROADMANIFESTPAGEPLUS',
  commitPlus: 'ROADMANIFESTPAGEPLUS_COMMIT',
};

export const allUrl = {
  // userList: 'tms/tms-bill/selectUserList_s',
  userList: 'mds/mds-road-manifest/selectUser_s',
  userDetail: 'mds/mds-user/viewDetails',
};

export const routeUrl = {
  detail: '/customs/declareList/declareDetail',
  detailPlus: '/customs/declareListPlus/declareDetailPlus',
};

export const allDispatchType = {
  list: 'declare/selectDeclareList', //报关列表
  detail: 'declare/selectDeclareDetail', //报关详情
  deliveryList: 'declare/selectDeliveryList', //提运单列表
  detailsDelivery: 'declare/detailsDelivery', //提运单详情
  value: 'declare/allValus',
  save: 'declare/saveDeclare',
  goodsList: 'declare/selectGoodsList', //商品项列表
  containerList: 'declare/selectContainerList', //集装箱列表
  containerByIdsList: 'declare/selectContainerByIdsList',
  ICcardList: 'declare/selectIcCardList',
  getMdsDeliveryInfo: 'declare/getMdsDeliveryInfo',//运单总货重


  listPlus: 'declare/selectManifestPlusList', //报关列表--预配舱单
  detailPlus: 'declare/selectManifestPlusDetail', //报关详情--预配舱单
  deliveryListPlus: 'declare/mdsDeliveryList', //提运单列表--预配舱单
  detailsDeliveryPlus: 'declare/mdsDelivery', //提运单详情--预配舱单 mdsDeliveryList 
  savePlus: 'declare/mdsSave',//提交--预配舱单

};

export function renderTableAdSelect({ key, data, value, props, declare }) {
  let params = { onlyRead: true, value };
  if (key) {
    const { dictObject } = props;
    params = { data: dictObject[key], payload: { code: [key] }, ...params };
  } else {
    params = { data, ...params };
  }
  let show = { id: 'code', name: 'value' };
  if (declare) {
    show = { id: 'value', name: 'code' };
  }
  params.show = show;
  return <AdSelect {...params}/>;
}

export function renderTableAdSearch({ value, props }) {
  if (!value || !searchValue) return '';
  const { searchValue } = props;
  const params = {
    onlyRead: true,
    value: searchValue[value],
    label: 'loginName',
    name: 'sysName',
  };
  return <AdSearch {...params}  />;
}

export function selectDeclareList({ payload = {}, props, call }) {
  const { dispatch, searchValue } = props;
  dispatch({
    type: allDispatchType.list,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
      console.log('call===', call);
      if (call) router.push(`${routeUrl.detail}/${call}`);
    },
  });
}
export function selectPlusList({ payload = {}, props, call }) {
  const { dispatch, searchValue } = props;
  dispatch({
    type: allDispatchType.listPlus,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
      // console.log('call===', call);
      if (call) router.push(`${routeUrl.detailPlus}/${call}`);
    },
  });
}

export function selectInfoList({ type, payload, props }) {
  const { dispatch } = props;
  dispatch({
    type,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
    },
  });
}

export function selectDeclareDetailAndInfo({ type, payload, props, callback }) {
  const { dispatch } = props;
  dispatch({
    type,
    payload,
    callback: data => {
      if (!data) return;
      callback && callback(data);
    },
  });
}


export function saveAllValues({ payload = {}, props } = {}) {
  const { dispatch } = props;
  dispatch({
    type: allDispatchType.value,
    payload,
  });
}
